package com.xyht.sca_s.student_manage_system.modules.visitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.TimeUtil;
import com.xyht.sca_s.student_manage_system.modules.qr.entity.SmsQr;
import com.xyht.sca_s.student_manage_system.modules.qr.mapper.SmsQrMapper;
import com.xyht.sca_s.student_manage_system.modules.visitor.entity.SmsVisitorManage;
import com.xyht.sca_s.student_manage_system.modules.visitor.entity.vo.SmsVisitorManageVO;
import com.xyht.sca_s.student_manage_system.modules.visitor.mapper.SmsVisitorManageMapper;
import com.xyht.sca_s.student_manage_system.modules.visitor.service.SmsVisitorManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.qr.constant.QrTypeConstant.QR_TYPE_VISITOR;

@Service
public class SmsSmsVisitorManageImpl extends ServiceImpl<SmsVisitorManageMapper, SmsVisitorManage> implements SmsVisitorManageService {
    @Autowired
    SmsQrMapper smsQrMapper;
    @Autowired
    SmsVisitorManageMapper smsVisitorManageMapper;

    @Override
    public ResponseResult visitorAdd(SmsVisitorManageVO smsVisitorManageVO) {
        if (isNullOrEmpty(smsVisitorManageVO.getVisitorName()) || isNullOrEmpty(smsVisitorManageVO.getVisitorNum()) ||
                isNullOrEmpty(smsVisitorManageVO.getVisitorContactsName())
                || isNullOrEmpty(smsVisitorManageVO.getVisitorContactsPhone()) || isNullOrEmpty(smsVisitorManageVO.getVisitorPhoneNum())
                || isNullOrEmpty(smsVisitorManageVO.getVisitorEntranceTime())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        QueryWrapper<SmsQr> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(SmsQr::getId, smsVisitorManageVO.getQrId())
                .eq(SmsQr::getQrType, QR_TYPE_VISITOR);
        SmsQr smsQr = smsQrMapper.selectOne(queryWrapper);
        if (isNullOrEmpty(smsQr)) {
            return CommonResult.failed(CommonCodeEnum.QR_NOT_EXIST);
        }
        SmsVisitorManage smsVisitorManage = new SmsVisitorManage();
        smsVisitorManage.setVisitorName(smsVisitorManageVO.getVisitorName());
        smsVisitorManage.setVisitorCarNum(smsVisitorManageVO.getVisitorCarNum());
        smsVisitorManage.setVisitorNum(smsVisitorManageVO.getVisitorNum());
        smsVisitorManage.setVisitorContactsPhone(smsVisitorManageVO.getVisitorContactsPhone());
        smsVisitorManage.setVisitorEntranceGuardName(smsVisitorManageVO.getVisitorEntranceGuardName());
        smsVisitorManage.setVisitorPhoneNum(smsVisitorManageVO.getVisitorPhoneNum());
        smsVisitorManage.setVisitorContactsName(smsVisitorManageVO.getVisitorContactsName());
        smsVisitorManage.setVisitorEntranceGuardName(smsQr.getQrPosition());
        smsVisitorManage.setVisitorEntranceTime(smsVisitorManageVO.getVisitorEntranceTime());
        smsVisitorManage.setQrId(smsQr.getId());
        smsVisitorManageMapper.insert(smsVisitorManage);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deleteVisitor(SmsVisitorManageVO smsVisitorManageVO) {
        if (isNullOrEmpty(smsVisitorManageVO.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsVisitorManage queryResult = smsVisitorManageMapper.selectById(smsVisitorManageVO.getId());
        if (isNullOrEmpty(queryResult)) {
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        smsVisitorManageMapper.deleteById(smsVisitorManageVO.getId());
        return CommonResult.success();
    }


    @Override
    public ResponseResult selectVisitor(String search_str,Integer pageSize, Integer pageNum, String bg_date, String end_date) {
        Date bgDate = null;
        Date endDate = null;
        if (!isNullOrEmpty(bg_date)) {
            bgDate = TimeUtil.StringtoDate(bg_date);
        }
        if (!isNullOrEmpty(end_date)) {
            endDate = TimeUtil.StringtoDate(end_date);

        }
        QueryWrapper<SmsVisitorManage> queryWrapper = new QueryWrapper<>();
        if (isNullOrEmpty(queryWrapper)) {
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        if (bgDate != null) {
            queryWrapper.lambda().ge(SmsVisitorManage::getCreateTime, bgDate);
        }
        if (endDate != null) {
            queryWrapper.lambda().le(SmsVisitorManage::getCreateTime, endDate);
        }

        if (!isNullOrEmpty(search_str)) {
            queryWrapper.lambda().and(Wrapper -> {
                Wrapper.like(SmsVisitorManage::getVisitorName, search_str)
                        .or()
                        .like(SmsVisitorManage::getVisitorEntranceGuardName, search_str);
            })
            ;
        }
        List<SmsVisitorManage> list1 = smsVisitorManageMapper.selectList(queryWrapper);
        if (list1.size() == 0) {
            return null;
        }
        queryWrapper.orderByDesc("create_time");
        Page<SmsVisitorManage> page = new Page<>(pageNum, pageSize);

        smsVisitorManageMapper.selectPage(page, queryWrapper);
        List<SmsVisitorManage> list = page.getRecords();
        int total = (int) page.getTotal();

        return CommonResult.success(list, total);
    }


}

